package com.youcai.springboot_day03.dao;


import com.youcai.springboot_day03.entity.Department;
import com.youcai.springboot_day03.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class EmployeeDaoImpl {

    private static Map<Integer, Employee> employeeMap = null;

    @Autowired
    private DepartmentDaoImpl departmentDao;


    static {

        employeeMap = new HashMap<Integer, Employee>();

        employeeMap.put(1001,new Employee(1001,"AA","3361144574@qq,com",1,new Department(105,"小卖部")));
        employeeMap.put(1002,new Employee(1002,"BB","3361144574@qq,com",1,new Department(105,"小卖部")));
        employeeMap.put(1003,new Employee(1003,"CC","3361144574@qq,com",1,new Department(105,"小卖部")));
        employeeMap.put(1004,new Employee(1004,"DD","3361144574@qq,com",1,new Department(105,"小卖部")));
        employeeMap.put(1005,new Employee(1005,"EE","3361144574@qq,com",1,new Department(105,"小卖部")));
        employeeMap.put(1006,new Employee(1006,"FF","3361144574@qq.com",0,new Department(105,"小卖部")));

    }
    //eid自增长
    public static Integer initid = 1007;


    //添加一个
    public void savaEmployee(Employee employee){
        if (employee.getEid() == null){
            employee.setEid(initid ++);
        }

        employee.setDepartment(departmentDao.departmentByDno(employee.getDepartment().getDno()));

        employeeMap.put(employee.getEid(),employee);
    }


    //查询全部
    public Collection<Employee> getAll(){
        return employeeMap.values();
    }

    //id查询
    public Employee employeeByEid(Integer eid){
        return employeeMap.get(eid);
    }

    //删除
    public void deleteEmployee(Integer eid){
        employeeMap.remove(eid);
    }

}
